package operation.modules.workReport.controller;

import operation.modules.workReport.entity.WorkDateEntity;
import operation.modules.workReport.service.WorkDateService;
import operation.util.ReqResult;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* 日期 控制层
*/
@RestController
@RequestMapping("api/work/date")
public class WorkDateController {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private WorkDateService workDateService;

    /**
     * 查询所有
     */
    @RequestMapping("/query-all")
    public ReqResult queryAll() {
        logger.info("日期表 查询所有数据");
        List<WorkDateEntity> list = workDateService.queryAll();
        return ReqResult.ok().put("list", list);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ReqResult save(@RequestBody WorkDateEntity workDateEntity) {
        logger.info("日期表 保存信息");
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(workDateEntity.getWorkShift());
        workDateEntity.setYear(time.substring(2,4));
        workDateEntity.setMonth(time.substring(5,7));
        workDateEntity.setDay(time.substring(8,10));
        workDateEntity.setCode("D" + time.substring(0,4) + time.substring(5,7) + time.substring(8,10));
        workDateEntity.setCreateTime(new Date());
        if (workDateEntity.getTitle().equals("添加日期信息")) {
            workDateService.save(workDateEntity);
        } else {
            workDateService.updateById(workDateEntity);
        }
        return ReqResult.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ReqResult delete(@RequestBody Map<String, Object> params) {
        logger.info("日期表 根据id删除");
        int id = (Integer) params.get("id");
        workDateService.removeById(id);
        return ReqResult.ok();
    }
}